package linkedlist;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 十八岁讨厌编程
 * @date 2025/6/27 21:13
 * @PROJECT_NAME leetcode
 * @description
 */

public class Solution142 {

    public ListNode detectCycle(ListNode head) {
        Set<ListNode> nodeSet = new HashSet();
        ListNode pointer = head;
        while(pointer != null) {
            if(nodeSet.contains(pointer)) return pointer;
            nodeSet.add(pointer);
            pointer = pointer.next;
        }
        return null;
    }

}
